var api = require("../../../api.js"),
  app = getApp(),
  qqmapsdk,
  QQMapWX = require('../../../utils/qqmap-wx-jssdk.min.js');
const {
  $Toast
} = require('../../../iview/base/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    com_show: false,
    address_autosize: {
      maxHeight: 100,
      minHeight: 50
    },
    checked: false,
    name: '',
    industry: '',
    phone: '',
    contactsName: '',
    detail: '',
    ssxq: '',
    address: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      /** 自己的key秘钥 http://lbs.qq.com/console/mykey.html 在这个网址申请 */
      key: 'T7MBZ-4EBC6-E3ZSY-EFZMG-BP5RF-FCFOC'
    });
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  add_comp() {
    if (!this.data.checked) {
      $Toast({
        content: '请同意会员协议',
      });
    } else {
      this.setData({
        com_show: true
      });
    }
  },
  onClose() {
    this.setData({
      com_show: false
    });
  },
  bindKeyInput: function (e) {
    let item = e.currentTarget.dataset.model;
    this.setData({
      [item]: e.detail
    });
  },
  /** 选择省市县区 */
  sel_add() {
    var that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.chooseLocation({
                success: res => {
                  that.getAddress(res.latitude, res.longitude)
                }
              })
            },
            /** 失败 */
            fail() {}
          })
        } else {
          // 已经授权了就会直接进入地图
          wx.chooseLocation({
            success: res => {
              that.getAddress(res.latitude, res.longitude)
            }
          })
        }
      }
    })
  },
  /** 获取地理位置 */
  getAddress: function (latitude, longitude) {
    let that = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        let district = res.result.ad_info.district
        let detail = res.result.formatted_addresses.recommend
        that.setData({
          ssxq: `${province} ${city} ${district}`,
          latitude: latitude,
          longitude: longitude,
          detail: detail,
          address: `${province}${city}${district}${detail}`,
        })
      }
    });
  },

  /** 提交公司认证 */
  auth_comp: function () {
    let app_id = app.getOpenId();
    if(app_id == undefined){
      $Toast({
        content: '授权出错，请重新授权',
      });
      return
    }
    if (!this.data.name.length == 0 && !this.data.industry.length == 0 && !this.data.phone.length == 0 && !this.data.contactsName.length == 0 && !this.data.address.length == 0) {
      let sendData = {
        openId: app_id,
        name: this.data.name,
        industry: this.data.industry,
        phone: this.data.phone,
        contactsName: this.data.contactsName,
        address: this.data.address,
      }
      wx.request({
        url: api.company.creatCompany,
        data: sendData,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success(res) {
          console.log(res)
          if (res.data.code === 200) {
            /** 提交付费 */
            let param = {
              openId: app.globalData.wxUserInfo.openId,
              money: 0.01,
              paymentMoney: 0.01,
              body: '会员认证费用',
              type: 3,
            }
            wx.request({
              url: api.active.tobuy,
              data: param,
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              method: 'POST',
              success(res) {
                if (res.data.code == 200) {
                  /** 支付返回 */
                  wx.requestPayment({
                    timeStamp: res.data.data.timeStamp,
                    nonceStr: res.data.data.nonceStr,
                    package: res.data.data.package,
                    signType: res.data.data.signType,
                    paySign: res.data.data.paySign,
                    success(res) {},
                    fail(res) {},
                    /** 方法完成事件 */
                    complete(res) {
                      /** 支付成功 */
                      if (res.errMsg == "requestPayment:ok") {
                        /** 延迟1s 提示 */
                        setTimeout(() => {
                          $Toast({
                            content: '支付成功',
                            type: 'success'
                          });
                        }, 500);
                        /** 返会首页 */
                        wx.redirectTo({
                          url: '/pages/index/index',
                        })
                      } else {
                        $Toast({
                          content: '未支付，认证已提交',
                        });
                       
                      }
                    }
                  })
                } else {
                  $Toast({
                    content: '未支付，认证已提交',
                  });
                }
              }
            });
          }
          setTimeout(()=>{
            _this.setData({
              com_show: false
            });
          },1200)
        }
      });
    } else {
      $Toast({
        content: '数据填写不完整',
        icon: 'delete'
      });
    }
  },
})